<?php
// +----------------------------------------------------------------------
// | 车保点点首页
// +----------------------------------------------------------------------
// | Author: cart <469714167@qq.com>
// +----------------------------------------------------------------------
// | Datetime: 2018-2-1 09:00:00
// +----------------------------------------------------------------------
// | Copyright: 微量科技
// +----------------------------------------------------------------------
namespace app\ice\controller;

use think\Loader;
use app\wx\model\Area;
use think\Exception;
use think\Cache;

class Bind extends Init
{
    private $UserModel;
    /**
     * [_initialize 初始化]
     * @return [type] [description]
     * @author carl
     * @copyright wei liang
     */
    protected function _initialize()
    {
        parent::_initialize();
        $this->UserModel = Loader::model('User');
    }

    // 找回密码
    public function bindPhone()
    {
        $param = input('post.');
        $result = $this->validate($param, 'BindValidate');
            if (true !== $result) {
                return ['code'=>400,'message'=>$result,'data'=>[],'status'=>false];
            }
            if ($param['yzmcode'] != Cache::get('reset' . $param['phone'])) {
                return ['code'=>400,'message'=>'验证码错误','data'=>[],'status'=>false];
            }
        $res = $this->UserModel->getUserData($param['openid']);
        if($res){
            $rs = $this->UserModel->where(['openid' => $param['openid']])->save(['phone' => $param['phone']  ]);
        }else{
            $rs = '';
        }
            if($rs){
                $getPhone = $this->UserModel->getUserData($param['openid']);
                return ['code'=>20003,'message'=>'绑定成功','data'=>$getPhone,'status'=>true];
            }else{
                return ['code'=>40012,'message'=>'绑定失败','data'=>[],'status'=>false];
            }
    }

    // 验证码
    public function sendSms()
    {
            $param = input('post.');
            if (empty($param['phone'])) {
                return ['code'=>400,'message'=>'手机号必须填写','data'=>[],'status'=>false];
            }
            $rs = $this->UserModel->where(['phone' => $param['phone']])->count();
            // 发验证码(类型) 1是注册 2是找回(验证码 阿里大鱼)
            if ($rs) {
                return ['code'=>400,'message'=>'账号已经存在','data'=>[],'status'=>false];
            }
            // 短信内容参数
            $smsParams = [
                'code' => randString(),
                'product' => config('ProjectName')
            ];
            $return = sendSms($smsParams, 'SMS_124455059', $param['phone']);
            $cacheName = 'reset' . $param['phone'];

            if (isset($return['alibaba_aliqin_fc_sms_num_send_response']) && $return['alibaba_aliqin_fc_sms_num_send_response']['result']['success'] == 1) {
                if (Cache::set($cacheName, $smsParams['code'], 300)) {
                    return ['code'=>200,'message'=>'验证码发送成功','data'=>[],'status'=>true];
                } else {
                    return ['code'=>400,'message'=>'验证码发送失败','data'=>[],'status'=>false];
                }
            } else {
                return ['code'=>400,'message'=>$return['error_response']['sub_msg'],'data'=>[],'status'=>false];
            }
    }

    // 验证码
    public function sendSms1()
    {
        // $ReturnCode = new ReturnCode;
        $param = input('post.');
            if (empty($param['phone']) ) {
                return ['code'=>400,'message'=>'验证码类型错误','data'=>[],'status'=>false];
            }
        $rs = $this->UserModel->where(['phone' => $param['phone']])->count();
        // 短信内容参数
        $smsParams = [
            'code' => randString(),
            'product' => config('ProjectName')
        ];
        $return = sendSms($smsParams, 'SMS_13745536', $param['phone']);
        $cacheName = 'reset' . $param['phone'];
            if (isset($return['alibaba_aliqin_fc_sms_num_send_response']) && $return['alibaba_aliqin_fc_sms_num_send_response']['result']['success'] == 1) {
                if (Cache::set($cacheName, $smsParams['code'], 300)) {
                    return ['code'=>200,'message'=>'验证码发送成功','data'=>[],'status'=>true];
                } else {
                    return ['code'=>400,'message'=>'验证码发送失败','data'=>[],'status'=>false];
                }
            } else {
                return ['code'=>400,'message'=>$return['error_response']['sub_msg'],'data'=>[],'status'=>false];
            }
    }

    /**
     * [bind 绑定微信公众号车保点点]
     * 
     * @return [type] [description]
     * @author polo
     * @copyright wei liang
     */
    public function bind() {
        // $weObj = new Wechat ( self::$options );
        $json = $this->wechat->getOauthAccessToken();
        if (!$json) {
            return ['code'=>400,'message'=>'code不正确','data'=>[],'status'=>true];
        }
        $useropenid = $json["openid"];
        $userinfo = $this->wechat->getUserInfo ( $useropenid );
        if ($userinfo ['subscribe'] == 0) {
            // 查询该微信用户在我们服务器内的数据
            $wechat_info = $this->UserModel->getUserData ( $useropenid );
            if (empty ( $wechat_info )) {
                $data = array (
                        'openid' => $userinfo ['openid'],
                        'subscribe' => 1,
                        'status' => ( int ) 1
                );
                $this->UserModel->addUser( $data );
                $wechat_info = $this->UserModel->getUserData ( $useropenid );
                return ['code'=>200,'message'=>'查询成功','data'=>$wechat_info,'status'=>true];
            } else{
                return ['code'=>200,'message'=>'查询成功','data'=>$wechat_info,'status'=>true];
            }
        }else{
            $wechat_info = $this->UserModel->getUserData ( $useropenid );
            if (empty ( $wechat_info )) {
                $data = array (
                    'openid' => $userinfo['openid'],
                    'nickname' => $userinfo['nickname'],
                    'sex' => $userinfo['sex'],
                    'language' => $userinfo ['language'],
                    'city' => $userinfo['city'],
                    'province' => $userinfo['province'],
                    'haedimgurl' => $userinfo['headimgurl'],
                    'country' => $userinfo['country'],
                    'intime' => time(),
                    'ubscribe' => $userinfo['subscribe'],
                    'subscribe_time' => $userinfo['subscribe_time'],
                );
                $this->UserModel->addUser( $data );
                $wechat_info = $this->UserModel->getUserData ( $useropenid );
                return ['code'=>200,'message'=>'查询成功','data'=>$wechat_info,'status'=>true];
            }else{
                return ['code'=>200,'message'=>'查询成功','data'=>$wechat_info,'status'=>true];
            }
        }
    }
}